package cn.gjxblog.demo;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.config.IniSecurityManagerFactory;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.subject.Subject;

import java.util.Arrays;

/**
 * 作用:
 * 作者:gjx
 * 版本:V1.0
 * 创建时间: 2018/7/25 22:40
 */
public class TestShiroPermission {

    public static void main(String[] args) {
        IniSecurityManagerFactory factory = new IniSecurityManagerFactory("classpath:shiro-permission.ini");
        SecurityManager securityManager = factory.getInstance();
        SecurityUtils.setSecurityManager(securityManager);
        Subject subject = SecurityUtils.getSubject();
        subject.login(new UsernamePasswordToken("libai","123456"));

        System.out.println(subject.isAuthenticated());

        //查询当前用户有没有这个角色
        boolean role1 = subject.hasRole("role3");
        System.out.println(role1);
        //查询当前用户是否有多个角色
        boolean[] booleans = subject.hasRoles(Arrays.asList("role1", "role2", "role3"));
        for (boolean aBoolean : booleans) {
            System.out.println(aBoolean);
        }
        //查询当前用户是否同时具有这些角色
        boolean hasAllRoles = subject.hasAllRoles(Arrays.asList("role1", "role2", "role3"));
        System.out.println(hasAllRoles);
        //查询是否含有某个权限
        boolean permitted = subject.isPermitted("user:create");
        System.out.println(permitted);

        //查询当前用户是否同时含有多个权限
        boolean permittedAll = subject.isPermittedAll("user:create", "user:update");
        System.out.println(permittedAll);
        

    }
}
